package net.yambal.mobile.adv.manager
{
	import flash.desktop.NativeApplication;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	import net.yambal.mobile.adv.ADV_Chapter;
	import net.yambal.mobile.adv.managed_component.ADV_ViewNavigatorApplication;
	
	import org.osmf.events.TimeEvent;

	public class ADV_AppMng
	{
		private static var _instance:ADV_AppMng = null;
		
		public var story:ADV_StoryMng = null;
		
		
		private var _vna:ADV_ViewNavigatorApplication = null;
		private var _dvice_size:Rectangle = null;
		private var _appStartTime:Number = 0;
		
		private var _storyXML:XML = null;
		
		public function ADV_AppMng()
		{
			var now:Date = new Date();
			_appStartTime = Date.UTC(now.fullYear, now.month, now.date, now.hours, now.minutes, now.seconds, now.milliseconds);
		}
		
		public static function getInstance():ADV_AppMng {
			if(_instance == null){
				_instance = new ADV_AppMng();
			}
			return _instance;
		}
		
		//
		public function onViewNaviAppCreationComplete(vna:ADV_ViewNavigatorApplication):void{
			trace(this + ":onViewNaviAppCreationComplete");
			
			_vna = vna;
			_dvice_size = new Rectangle(0,0,_vna.width, _vna.height);

			callLator(readValues);
		}
		
		private function readValues():void{
			trace(this + ":readValues");
			
			if(_vna.storyEmbedXMLCls != null){
				story = ADV_StoryMng.getInstance(_vna.storyEmbedXMLCls);

			}
			
			callLator(onReadedValues);
		}
		
		// --------------------------------------------------------------------------------
		/**
		 * 初期データの読み込みが完了したら...
		 * フラッシュ画面から（指定時間[flashShowMS]を経過していれば）遷移する
		 **/
		private function onReadedValues():void{
			var check:Number = _vna.flashShowMS - getAppTime();
			
			if(check <= 0){
				callLator(goHome);
			}else{
				trace(this + ":now " + getAppTime() + "...wait " + check + "ms");
				var ft:Timer = new Timer(check * 10);
				ft.addEventListener(TimerEvent.TIMER, onFlashShowTimer, false, 0, true);
				ft.start();
			}
		}
		
		private function onFlashShowTimer(e:TimerEvent):void{
			trace(this + ":onTimer");
			var ft:Timer = e.currentTarget as Timer;
			ft.stop();
			ft.removeEventListener(TimerEvent.TIMER, onFlashShowTimer);
			callLator(goHome);
		}
		
		/**
		 * ホーム画面に遷移
		 **/
		public function goHome():void{
			_vna.goHome();
		}
		
		public function pushChapterParagraph(chapter_id:String, paragraph_id:String = null, option:Object = null):void{
			trace(this + ":pushChapterParagraph(" + chapter_id + "," + paragraph_id + ")");
			_vna.pushChapterParagraph(chapter_id, paragraph_id, option);
		}
		
		/**
		 * 
		 **/
		public function exit():void{
			trace(this + ":exit");
			NativeApplication.nativeApplication.exit();
		}
		
		// 汎用 Function --------------------------------------------------------------------
		public function get app_width():Number{
			return _dvice_size.width;
			//return _dvice_size.height;
		}
		
		public function get app_height():Number{
			//return _dvice_size.width;
			return _dvice_size.height;
		}
		
		public function get horizontal_width():Number{
			if(app_width > app_height){
				return app_width;
			}
			return app_height;
		}
		
		public function get horizontal_height():Number{
			if(app_width < app_height){
				return app_width;
			}
			return app_height;
		}
		
		private function callLator(fnc:Function, aug:Array = null):void{
			if(_vna != null){
				_vna.callLater(fnc, aug);
			}else{
				trace("ERROR");
			}
		}
		
		public function getTimeStump():Number{
			var now:Date = new Date();
			return Date.UTC(now.fullYear, now.month, now.date, now.hours, now.minutes, now.seconds, now.milliseconds);
		}
		
		public function getAppTime():Number{
			return getTimeStump() - _appStartTime;
		}
		
		public function saveArray(name:String, arr:Array):void{
			var so:SharedObject = SharedObject.getLocal(name);
			so.data.arr = arr;
			so.flush();
		}
		
		public function pushArray(name:String, obj:Object):void{
			var arr:Array = loadArray(name);
			arr.push(obj);
			
			var so:SharedObject = SharedObject.getLocal(name);
			so.data.arr = arr;
			so.flush();
		}
		
		public function loadArray(name:String):Array{
			var so:SharedObject = SharedObject.getLocal(name);
			if(so.data == null || so.data.arr == null){
				saveArray(name, []);
				return [];
			}
			var ret:Array = so.data.arr as Array;
			return ret;
		}
	}
}